/*
 * sohu-scraper.js
 */

if (typeof Sohu_Scraper == "undefined") {
	var Sohu_Scraper = {
		_log : new Logger("Sohu_Scraper"),
		waitTime : 200,
		timeReg : new RegExp("(\\d+)-(\\d+)-(\\d+) (\\d+):(\\d+):(\\d+)", "m"),
		urlReg : new RegExp("club.*\\.sohu\\.com\\/r-.*[\\d|-]+\\.html", "i"),
		pageNumRegExp : new RegExp("\u4e0b\u4e00\u9875")
	};
}

Sohu_Scraper.execute = function(doc, data, isFirstPage) {
	try {
		Sohu_Scraper._log.debug("> execute");

		var blocks = new Array();// {eCONTENTS,eANCHOR,eTEXTNODE}

		// extract the 1~n threads.
		var xpath = "//div[@id='content']//script";
		var elmts = DOMUtils.selectNodes(doc, xpath);
		Sohu_Scraper._log.debug("elmts.length: " + elmts.length);

		var scripts_1 = new Array(), scripts_2 = new Array(), scripts_3 = new Array();
		for (var idx = 0; idx < elmts.length; idx++) {
			var elmt = elmts[idx];
			// Sohu_Scraper._log.debug(idx + ": " + elmt.textContent);
			if (elmt.textContent.indexOf("\"body\"") > 0) {
				scripts_1.push(elmt);
			} else if (elmt.textContent.indexOf("\"nick\"") > 0) {
				scripts_2.push(elmt);
			} else if (elmt.textContent.indexOf("\"inputdate\"") > 0) {
				scripts_3.push(elmt);
			}
		}

		Sohu_Scraper._log.debug("scripts_1.length: " + scripts_1.length);
		Sohu_Scraper._log.debug("scripts_2.length: " + scripts_2.length);
		Sohu_Scraper._log.debug("scripts_3.length: " + scripts_3.length);

		// 2008-7-26 20:46:06
		for (var i = 0; i < scripts_1.length; i++) {
			var block = {
				eCONTENTS : new Array(),
				eANCHOR : null,
				eTEXTNODE : null
			};
			var next = scripts_1[i].nextSibling;
			while (next && next.nodeName != "SCRIPT") {
				block.eCONTENTS.push(next);
				next = next.nextSibling;
			}

			var j = i * 2;
			if (scripts_2.length > j) {
				var next = scripts_2[i].nextSibling;
				for (var k = 0; k < 2 && next; k++) {
					if ((next.nodeType == Node.ELEMENT_NODE && next.nodeName == "A")) {
						block.eANCHOR = next;
						break;
					}
				}
			}

			if (scripts_3.length > i) {
				var next = scripts_3[i].nextSibling;
				for (var k = 0; k < 2 && next; k++) {
					if ((next.nodeType == Node.TEXT_NODE && Sohu_Scraper.timeReg
							.test(next.textContent))) {
						block.eTEXTNODE = next;
						break;
					}
				}
			}

			blocks.push(block);
		}

		var threads = new Array();
		for (var idx = 0; idx < blocks.length; idx++) {
			var block = blocks[idx];

			var thread = new Object();
			thread.title = "";

			// extract author.
			if (block.eANCHOR) {
				var author = block.eANCHOR.textContent.trim();
				thread.author = author;
				// Sohu_Scraper._log.debug(author);
			}

			// extract create time.
			if (block.eTEXTNODE) {
				var text = block.eTEXTNODE.textContent.trim();
				Sohu_Scraper.timeReg.exec(text);
				var createtime = RegExp.$1 + "-" + RegExp.$2 + "-" + RegExp.$3
						+ " " + RegExp.$4 + ":" + RegExp.$5 + ":" + RegExp.$6;
				thread.createtime = createtime;
				// Sohu_Scraper._log.debug(createtime);
			}

			// extract content.
			var cont = DOMUtils.getTrimHTML(block.eCONTENTS);
			thread.content = cont;

			threads.push(thread);
		}
		data.threads = threads;
		Sohu_Scraper._log.debug(threads.length + " threads are found.");

		// extract pages
		xpath = "//div[@id='head']/div/form/div/a";
		elmts = DOMUtils.selectNodes(doc, xpath);
		var pages = new Array();
		for (var i = 0; i < elmts.length; i++) {
			var eLink = elmts[i];
			Sohu_Scraper._log.debug(i + ": " + eLink.textContent);
			if (Sohu_Scraper.pageNumRegExp.test(eLink.textContent)) {
				pages.push(eLink.href + "");
			}
		}
		data.pages = pages;
		Sohu_Scraper._log.debug(pages.length + " page urls are found.");

		// extract topic.
		if (isFirstPage) {
			var titleReg = new RegExp("(.+),\u7b2c1\u9875", "m");
			titleReg.exec(doc.title + "");
			var topic = {
				title : (RegExp.$1 == "") ? doc.title + "" : RegExp.$1,
				author : threads[0].author,
				createtime : threads[0].createtime,
				url : doc.location.href + ""
			};
			data.topic = topic;
		}

		Sohu_Scraper._log.debug("< execute");
	} catch (e) {
		Sohu_Scraper._log.error("execute: " + e);
	}
};
